import http from './http'
// import router from '../router'
import store from '../store'
import { logOut } from '@/utils/index'
// import md5 from 'md5'
import { Toast } from 'vant'

const post = (url, params, showLoad = true) => {
  if (!params) params = {}

  if (showLoad) {
    store.commit('switch_loading', true)
  }
  console.log('请求接口：' + url)

  let header = {
    lang: 'zh-cn',
    token: store.state.token ? store.state.token : ''
  }
  return new Promise((resolve, reject) => {
    http.instance
      .post(url, params, {
        headers: header
      })
      .then((res) => {
        store.commit('switch_loading', false)
        if (res.status == 200) {
          if (res.data.code == 1) {
            resolve(res.data)
          } else {
            Toast(res.data.msg)
            console.log('！！错误接口：' + url)
            reject(res.data)
            if (res.data.code == -1) {
              //重新登陆
              logOut()
            }
          }
        } else {
          Toast.fail('Interface status exception:' + res.statusCode)
          console.log('Interface status exception:' + res.statusCode)
        }
      })
      .catch((err) => {
        if (err.response.status === 401) {
          logOut()
        }
        Toast.fail('timeout of 8000ms exceeded')
        store.commit('switch_loading', false)
      })
  })
}

export default { post }
